import { Injectable } from '@angular/core';
import { Observable } from "rxjs/Observable";
import { Http, Response, Headers } from '@angular/http';
import "rxjs/add/operator/map";
import "rxjs/add/operator/catch";
import { ResponseModel } from '../model/BaseModel/response-model';
import { Constant } from '../constant/constant';

@Injectable()
export class LoginService {

  private Header = new Headers({'Content-Type': 'application/json'});
  private constant = new Constant;

  constructor(public http: Http) {}

  public userLogin(username: string, password: string): Observable<ResponseModel> {
    return this.http.post(this.constant.url + '/api/v1/login ', JSON.stringify({'username': username,
      'password': password}), { headers: this.Header })
      .map((res: Response) => {
        return res.json() as ResponseModel;
      })
      .catch(this.handleError);
  }
  private handleError(error: any): Observable<any> {
    alert('服务器连接失败，请稍后重试');
    return Observable.throw(error.message || error);
  }
}

